﻿using FieldTools.Client.Business.Business;
using FieldTools.Client.Business.IBusiness;
using FieldTools.Client.Controllers;
using FieldTools.Client.WebApi.Controllers;
using FieldTools.Client.Entity.OneBot;
using FieldTools.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FieldTools.Client.Controller
{
    [Route("/[controller]/[action]")]
    public class RobotController : BaseApiController
    {
        private readonly ILogger<RobotController> _logger;
        private readonly IOneBotServerApiBus _oneBotServerApiBus;
        public RobotController(ILogger<RobotController> logger, IOneBotServerApiBus oneBotServerApiBus)
        {
            _logger = logger;
            _oneBotServerApiBus = oneBotServerApiBus;
        }

        [HttpPost]
        public string PostTest(object info)
        {
            var str = "调用了一次测试post，传入参数为：" + info.ToJson();
            _logger.LogWarning(str);
            return str;
        }

        /// <summary>
        /// 处理事件上报
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<string> HandlePostEvent(object info)
        {
            try
            {
                var jsonStr = info.ToString();
                var baseEventInfo = jsonStr?.ToObject<OneBotEventBaseInfo>();
                if (baseEventInfo != null)
                {
                    if (baseEventInfo.PostType == "message")
                    {
                        var msgInfo = jsonStr?.ToObject<OneBotEnevtMessageInfo>();
                        var res = await _oneBotServerApiBus.HandleMessageInfoAsync(msgInfo);
                        return res == null ? "" : res.ToJson(true);
                    }
                    else if (baseEventInfo.PostType == "notice")
                    {
                        var res = await _oneBotServerApiBus.HandleNoticeInfoAsync(jsonStr?.ToObject<OneBotEventNoticeInfo>());
                        return res == null ? "" : res.ToJson(true);
                    }
                    else if (baseEventInfo.PostType == "request")
                    {
                        var res = await _oneBotServerApiBus.HandleRequestInfoAsync(jsonStr?.ToObject<OneBotEventRequestInfo>());
                        return res == null ? "" : res.ToJson(true);
                    }
                    else if (baseEventInfo.PostType == "meta_event")
                    {
                        var res = await _oneBotServerApiBus.HandleMetaInfoAsync(jsonStr?.ToObject<OneBotEventMetaInfo>());
                        return res == null ? "" : res.ToJson(true);
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
                //throw;
            }
            
            return "";
        }
    }
}
